import { CommentActionType, CommentType } from './../reducer/comment';
import {
  getCommentsByIdAPI,
  CommentParamsType,
  sendCommentByIdAPI,
  SendCommentParamsType,
} from '@/api/comment';
import { AppDispatch } from '..';

export const getCommentsByIdAction = (params: CommentParamsType) => {
  return async (dispatch: AppDispatch) => {
    const res = await getCommentsByIdAPI(params);
    dispatch({ type: 'comment/saveComments', payload: res.data });
  };
};

export const clearCommentsAction = (): CommentActionType => {
  return { type: 'comment/clearComments' };
};

export const saveOriginCommentsAction = (comment: CommentType): CommentActionType => {
  return { type: 'comment/saveOriginComment', payload: comment };
};
export const clearOriginCommentsAction = (): CommentActionType => {
  return { type: 'comment/clearOriginComment' };
};
//   3. 异步Action
export const sendCommentByIdAction = (params: SendCommentParamsType) => {
  return async (dispatch: AppDispatch) => {
    const res = await sendCommentByIdAPI(params);
    console.log('res  ----->  ', res);
  };
};

export const getReplyCommentsByIdAction = (params: { type: 'c'; source: string }) => {
  return async (dispatch: AppDispatch) => {
    const res = await getCommentsByIdAPI(params);
    console.log('res  ----->  ', res);
    dispatch({ type: 'comment/saveReplyComments', payload: res.data });
  };
};
